package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zhusx.core.helper.Lib_SelectPhotoHelper;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Arrays;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.utils.AppManager;
import lumingweihua.future.cn.baselibgxh.utils.SharedPreference;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.network.UserManager;
import lumingweihua.future.cn.lumingweihua.person.LoginActivity;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;
import lumingweihua.future.cn.lumingweihua.widget.dialog.BottomListDialog;

/**
 * todo 个人资料
 * Author       zhusx
 * Created      2018/3/16 12:02
 */

public class MineInfoActivity extends BaseCompatActivity {
    @BindView(R.id.iv_head)
    FrescoImageView headImg;
    @BindView(R.id.tv_name)
    TextView tvName;

    Lib_SelectPhotoHelper helper;
    BottomListDialog dialog;
    final int REQUEST_ID = 514;
    File headImageFile;
    LoadData<String> uploadData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        headImg.setImageURI(UserManager.getInstance().avatar_url);
        tvName.setText(UserManager.getInstance().nickname);

        helper = new Lib_SelectPhotoHelper(this, 300, 300);
        dialog = new BottomListDialog(this).setData(_Arrays.asList("相册", "拍照"), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        helper._startSelectPhoto(MineInfoActivity.this);
                        break;
                    case 1:
                        helper._startTakePhoto(MineInfoActivity.this);
                        break;
                }
                dialog.dismiss();
            }
        });
        uploadData = new LoadData<>(LoadData.Api.个人中心头像昵称修改, this);
        uploadData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<String> t) {
                showToast(t.getData());
                UserManager.getInstance().avatar_url = "file://" + headImageFile.getPath();
                headImg.setImageURI(UserManager.getInstance().avatar_url);
                setResult(Activity.RESULT_OK);
            }
        });

    }

    @OnClick({R.id.layout_head, R.id.layout_name, R.id.tv_exit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_head:
                dialog.show();
                break;
            case R.id.layout_name:
                startActivityForResult(new Intent(v.getContext(), ModifyNameActivity.class), REQUEST_ID);
                break;
            case R.id.tv_exit:
                new AlertDialog.Builder(this).setMessage("确定退出当前帐号?").setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreference.saveToSP(MineInfoActivity.this, "userToken", "");
                        SharedPreference.saveToSP(MineInfoActivity.this, "userId", "");
                        MyApplication.getInstance().setUserToken("");
                        MyApplication.getInstance().setUserId("");
                        AppManager.getInstance().finishAllActivity();
                        startActivity(new Intent(MineInfoActivity.this, LoginActivity.class));
                    }
                }).create().show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID && resultCode == RESULT_OK) {
            tvName.setText(UserManager.getInstance().nickname);
            setResult(RESULT_OK);
        } else {
            helper._onActivityResult(requestCode, resultCode, data, new Lib_SelectPhotoHelper.onDataListener() {
                @Override
                public void onPhoto(File file) {
                    headImageFile = file;
                    uploadData._loadData("avatar_url", toBase64String(compressFile(file)));
                }
            });
        }
    }
}
